//
//  showOrderViewController.m
//  ZPSALE
//
//  Created by tmosport on 14-7-4.
//  Copyright (c) 2014年 tmosport. All rights reserved.
//

#import "showOrderViewController.h"
#import "addEvaluateViewController.h"
#import "UIImageView+WebCache.h"
#import "addShowOrderViewController.h"

@interface showOrderViewController ()
{
    UIView *emptyFooterView;
}
@property (strong, nonatomic) IBOutlet UITableView *itemListTV;
@property (nonatomic,strong) NSArray *logResult;

@end

@implementation showOrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"评价晒单";
    
    [self setNavigationItem];
    
}

-(void)setNavigationItem {
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)viewWillAppear:(BOOL)animated
{
    [self dataInit];
}
- (void)dataInit{
    
    NSDictionary *params = @{@"memberid": _memberId,
                             @"access_token": _token,
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:APIURL@"myzhenpin/evaluate/getCanEvaluateList.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.logResult = responseObject;
        if ([_logResult count]==0) {
            [self showNoDataViewWithKeyWord:@"暂无待评价商品"];
        } else {
            [self hideNoDataView];
        }

        [self.itemListTV reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO:(@"Error:%@", error);
    }];
}
-(void)showNoDataViewWithKeyWord:(NSString *)key {
    NSString *str = [NSString stringWithFormat:@"%@",key];
    NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:str];
    //    [textStr addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0xff4400) range:NSMakeRange(6, key.length)];
    
    if (emptyFooterView) {
        UILabel *label = (UILabel*)[emptyFooterView viewWithTag:800];
        label.attributedText = textStr;
        emptyFooterView.hidden = NO;
        [self.view bringSubviewToFront:emptyFooterView];
    } else {
        int height;
        height = 528;
        emptyFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
        imageView.center = CGPointMake(emptyFooterView.frame.size.width/2, (emptyFooterView.frame.size.height-220)/2);
        imageView.image = [UIImage imageNamed:@"no05"];
        [emptyFooterView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (emptyFooterView.frame.size.height-240)/2+imageView.frame.size.height, 320, 30)];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 800;
        label.attributedText = textStr;
        [emptyFooterView addSubview:label];
        emptyFooterView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:emptyFooterView];
    }
}
-(void)hideNoDataView {
    if (emptyFooterView) {
        emptyFooterView.hidden = YES;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_logResult count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    NSDictionary *sectionDic = [_logResult objectAtIndex:indexPath.section];
    UIImageView *gImg = (UIImageView *) [cell viewWithTag:1001];
    UILabel *bLabel = (UILabel *)[cell viewWithTag:1002];
    UILabel *pName = (UILabel *)[cell viewWithTag:1003];
    UIButton *evaBT = (UIButton *)[cell viewWithTag:1004];//评价
    UIButton *showBT = (UIButton *)[cell viewWithTag:1005];//DO：晒单
    
    [evaBT addTarget:self action:@selector(toEva:) forControlEvents:UIControlEventTouchUpInside];
    [showBT addTarget:self action:@selector(toShow:) forControlEvents:UIControlEventTouchUpInside];
    [gImg sd_setImageWithURL:Return_imageUrl(sectionDic[@"imageSource"], 180) placeholderImage:[UIImage imageNamed:@"noimage"] options:SDWebImageRetryFailed];
    if ([sectionDic[@"isEvaluate"] integerValue] == 1) {
        [evaBT setBackgroundImage:[UIImage imageNamed:@"btnBgUesd"] forState:UIControlStateNormal];
        [evaBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        evaBT.enabled = NO;
    } else {
        [evaBT setBackgroundImage:[UIImage imageNamed:@"btnBg"] forState:UIControlStateNormal];
        [evaBT setTitleColor:ColorFromRGB(0xff4400) forState:UIControlStateNormal];
        evaBT.enabled = YES;
    }
    if ([sectionDic[@"isShowOrder"] integerValue] == 1) {
        [showBT setBackgroundImage:[UIImage imageNamed:@"btnBgUesd"] forState:UIControlStateNormal];
        [showBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        showBT.enabled = NO;
    } else {
        [showBT setBackgroundImage:[UIImage imageNamed:@"btnBg"] forState:UIControlStateNormal];
        [showBT setTitleColor:ColorFromRGB(0xff4400) forState:UIControlStateNormal];
        showBT.enabled = YES;
    }
    bLabel.text = [NSString stringWithFormat:@"%@",sectionDic[@"brandname"]];
    pName.text = [NSString stringWithFormat:@"%@",sectionDic[@"productName"]];
    return cell;
}


- (void)toEva:(UIButton *)sender {
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.itemListTV];
    NSIndexPath *ip = [self.itemListTV indexPathForRowAtPoint:buttonPosition];//获取按钮所在cellIP
    NSDictionary *sectionDic = [_logResult objectAtIndex:ip.section];
    
    NSInteger orderitemid = [sectionDic[@"orderItemId"] integerValue];
    addEvaluateViewController *adEvaVC = [self.storyboard instantiateViewControllerWithIdentifier:@"addEvaluateViewController"];
    adEvaVC.memberId = _memberId;
    adEvaVC.token = _token;
    adEvaVC.orderitemid = orderitemid;
    [self.navigationController pushViewController:adEvaVC animated:YES];
    
}

- (void)toShow:(UIButton *)sender {
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.itemListTV];
    NSIndexPath *ip = [self.itemListTV indexPathForRowAtPoint:buttonPosition];//获取按钮所在cellIP
    NSDictionary *sectionDic = [_logResult objectAtIndex:ip.section];
    
    NSInteger orderitemid = [sectionDic[@"orderItemId"] integerValue];
    addShowOrderViewController *addShowVC = [self.storyboard instantiateViewControllerWithIdentifier:@"addShowOrderViewController"];
    addShowVC.memberId = _memberId;
    addShowVC.token = _token;
    addShowVC.orderitemid = orderitemid;
    [self.navigationController pushViewController:addShowVC animated:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//#pragma mark - Navigation
//
//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//
//}


@end
